package com.music.playlist.controllers;

import static com.music.playlist.constants.PlaylistConstants.EXCEPTION_MESSAGE;
import static com.music.playlist.constants.PlaylistConstants.MY_PLAYLISTS;
import static com.music.playlist.constants.PlaylistConstants.RECENT_PLAYLISTS;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.music.client.playlist.client.PlaylistClient;
import com.music.playlist.logging.PlaylistLogger;
import com.music.service.members.beans.Member;
import com.music.service.playlists.beans.Playlist;

public class PlaylistHomeController implements Controller {

	private PlaylistClient mPlaylistClient;
	
	private String mViewName;
	
	private String mExceptionView;
	
	private static final Logger log = PlaylistLogger.getLogger();
	public ModelAndView handleRequest(HttpServletRequest pRequest,HttpServletResponse pResponse) 
					throws ServletException, IOException {
		log.debug("Entering PlaylistHomeController...");
		System.out.println("Entering PlaylistHomeController...");
		ModelAndView modelAndView = new ModelAndView();
		Long memberId = null;
		
		memberId = getMemberId(pRequest);
		log.debug("memberId="+memberId);
		if(memberId == null){
			return new ModelAndView(getExceptionView(),EXCEPTION_MESSAGE,"Invalid MemberId ");
		}
		
		List<Playlist> myPlaylist = null;
		List<Playlist> recentPlaylist = null;
		try {
			myPlaylist = getPlaylistClient().viewPlaylists(memberId);
			recentPlaylist = getPlaylistClient().getRecentPlaylists(memberId);
			if(myPlaylist != null){
				log.debug("Number of playlists="+myPlaylist.size());
				System.out.println("Number of playlists="+myPlaylist.size());
			}
		} catch (Exception e) {
			log.error("Error in fetching playlists for member="+memberId,e);
			e.printStackTrace();
		}
		modelAndView.addObject(MY_PLAYLISTS,myPlaylist);
		modelAndView.addObject(RECENT_PLAYLISTS,recentPlaylist);
		modelAndView.setViewName(getViewName());
		
		return modelAndView;
	}

	/**
	 * 
	 * @param pRequest
	 * @return
	 */
	private Long getMemberId(HttpServletRequest pRequest){
		Member member = (Member) pRequest.getAttribute("member");
		return member.getMemberId();
	}
	
	
	/**
	 * @return the viewName
	 */
	public String getViewName() {
		return mViewName;
	}

	/**
	 * @param pViewName the viewName to set
	 */
	@Required
	public void setViewName(String pViewName) {
		mViewName = pViewName;
	}

	/**
	 * @return the playlistClient
	 */
	public PlaylistClient getPlaylistClient() {
		return mPlaylistClient;
	}

	/**
	 * @param pPlaylistClient the playlistClient to set
	 */
	@Required
	public void setPlaylistClient(PlaylistClient pPlaylistClient) {
		mPlaylistClient = pPlaylistClient;
	}

	/**
	 * @return the exceptionView
	 */
	public String getExceptionView() {
		return mExceptionView;
	}

	/**
	 * @param pExceptionView the exceptionView to set
	 */
	@Required
	public void setExceptionView(String pExceptionView) {
		mExceptionView = pExceptionView;
	}

	
}

